package com.cmf.myproject.service.task;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmf.myproject.dal.mybatis.filters.TaskQueryFilter;
import com.cmf.myproject.dal.mybatis.mymodel.WeeklyViewRspDto;
import com.cmf.myproject.service.task.dto.MergeColumn;
import com.system.core.date.DateFormatString;
import com.system.core.view.layui.table.TableViewRspDto;

@Service
public class TaskFileService {
	
	@Autowired
	private TaskService taskService;
	private SimpleDateFormat dateFormat = new SimpleDateFormat(DateFormatString.dateString);
	private SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy.MM.dd");
	private List<MergeColumn> merges1 = new ArrayList<MergeColumn>();
	private List<MergeColumn> merges2 = new ArrayList<MergeColumn>();
	
	public void weeklyExport(TaskQueryFilter filter,OutputStream stream) {
		filter.setExportFile(true);
		TableViewRspDto<WeeklyViewRspDto> taskList = taskService.weeklyList(filter);
		XSSFWorkbook workbook = new XSSFWorkbook();
		
//		HSSFPalette palette = workbook.getCustomPalette();
//		palette.setColorAtIndex(IndexedColors.LIME.index, (byte) 169, (byte) 208, (byte) 142);
		
		XSSFFont headerFont = workbook.createFont();
		headerFont.setFontName("微软雅黑");
		headerFont.setBold(true);
		headerFont.setFontHeightInPoints((short) 12);
		
		XSSFFont bodyFont = workbook.createFont();
		bodyFont.setFontName("宋体");
		bodyFont.setBold(false);
		bodyFont.setFontHeightInPoints((short) 11);
		
		//设置头左对齐
		XSSFCellStyle headerLeft = workbook.createCellStyle();
		headerLeft.setAlignment(HorizontalAlignment.LEFT);
		headerLeft.setVerticalAlignment(VerticalAlignment.CENTER);
		headerLeft.setWrapText(true);
		headerLeft.setBorderBottom(BorderStyle.THIN);
		headerLeft.setBorderTop(BorderStyle.THIN);
		headerLeft.setBorderRight(BorderStyle.THIN);
		headerLeft.setBorderLeft(BorderStyle.THIN);
		headerLeft.setFillForegroundColor(new XSSFColor(new Color(169,208,142)));
		headerLeft.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		headerLeft.setFont(headerFont);
		
		//设置头居中对齐
		XSSFCellStyle headerCenter = workbook.createCellStyle();
		headerCenter.setAlignment(HorizontalAlignment.CENTER);
		headerCenter.setVerticalAlignment(VerticalAlignment.CENTER);
		headerCenter.setWrapText(true);
		headerCenter.setBorderBottom(BorderStyle.THIN);
		headerCenter.setBorderTop(BorderStyle.THIN);
		headerCenter.setBorderRight(BorderStyle.THIN);
		headerCenter.setBorderLeft(BorderStyle.THIN);
		headerCenter.setFillForegroundColor(new XSSFColor(new Color(169,208,142)));
		headerCenter.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		headerCenter.setFont(headerFont);
		
		//左对齐
		XSSFCellStyle style1 = workbook.createCellStyle();
		style1.setAlignment(HorizontalAlignment.LEFT);
		style1.setVerticalAlignment(VerticalAlignment.CENTER);
		style1.setWrapText(true);
		style1.setBorderBottom(BorderStyle.THIN);
		style1.setBorderTop(BorderStyle.THIN);
		style1.setBorderRight(BorderStyle.THIN);
		style1.setBorderLeft(BorderStyle.THIN);
		style1.setFont(bodyFont);
		
		//居中
		XSSFCellStyle style2 = workbook.createCellStyle();
		style2.setAlignment(HorizontalAlignment.CENTER);
		style2.setVerticalAlignment(VerticalAlignment.CENTER);
		style2.setWrapText(true);
		style2.setBorderBottom(BorderStyle.THIN);
		style2.setBorderTop(BorderStyle.THIN);
		style2.setBorderRight(BorderStyle.THIN);
		style2.setBorderLeft(BorderStyle.THIN);
		style2.setFont(bodyFont);
		
		XSSFSheet sheet = workbook.createSheet(dateFormat.format(Calendar.getInstance().getTime()));
		
		//设置宽度
		sheet.setColumnWidth(0, 20*256);
		sheet.setColumnWidth(1, 20*256);
		sheet.setColumnWidth(2, 60*256);
		sheet.setColumnWidth(3, 65*256);
		sheet.setColumnWidth(4, 35*256);
		sheet.setColumnWidth(5, 25*256);
		sheet.setColumnWidth(6, 40*256);
		sheet.setColumnWidth(7, 40*256);
		sheet.setColumnWidth(8, 35*256);
		
		//设置标题
		String[] title = new String[] {"岗位","姓名","工作事项","描述","开始时间-完成时间","当前进度","工作成果/产出","评分","备注"};
		XSSFRow row = sheet.createRow(0);
		row.setHeight((short)600);
		for(int i=0;i<title.length;i++) {
			XSSFCell titleCell = row.createCell(i);
			titleCell.setCellValue(title[i]);
			
			if(i==0 || i==1 || i==5 || i==6 || i==7) {
				titleCell.setCellStyle(headerCenter);
			} else {
				titleCell.setCellStyle(headerLeft);
			}
		}
		int rowNumber = 1;
		
		for(WeeklyViewRspDto weeklyViewRspDto:taskList.getData()) {
			XSSFRow dataRow = sheet.createRow(rowNumber);
			dataRow.getCTRow().setCustomHeight(false);
			dataRow.setHeight((short)500);
			XSSFCell cell0 = dataRow.createCell(0);
			cell0.setCellValue(weeklyViewRspDto.getPosition());
			cell0.setCellStyle(style2);
			merge1(cell0,merges1);
			
			XSSFCell cell1 = dataRow.createCell(1);
			cell1.setCellValue(weeklyViewRspDto.getNick());
			cell1.setCellStyle(style2);
			merge2(cell1,merges2);
			
			XSSFCell cell2 = dataRow.createCell(2);
			cell2.setCellStyle(style1);
			cell2.setCellValue(weeklyViewRspDto.getName());
			XSSFCell cell3 = dataRow.createCell(3);
			cell3.setCellStyle(style1);
			cell3.setCellValue(weeklyViewRspDto.getRemarkTask());
			XSSFCell cell4 = dataRow.createCell(4);
			String startTmp = dateParse(weeklyViewRspDto.getCodeStart());
			String endTmp = dateParse(weeklyViewRspDto.getCodeEnd());
			cell4.setCellValue((startTmp==""?"":startTmp+" ")+"-"+(endTmp==""?"":" "+endTmp));
			cell4.setCellStyle(style1);
			XSSFCell cell5 = dataRow.createCell(5);
			cell5.setCellValue(progress(weeklyViewRspDto.getProgress()));
			style2.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
			cell5.setCellStyle(style2);
			XSSFCell cell6 = dataRow.createCell(6);
			cell6.setCellStyle(style2);
			XSSFCell cell7 = dataRow.createCell(7);
			cell7.setCellValue("B");
			cell7.setCellStyle(style2);
			XSSFCell cell8 = dataRow.createCell(8);
			cell8.setCellValue(weeklyViewRspDto.getPendingReson());
			cell8.setCellStyle(style1);
			rowNumber ++;
		}
		for(MergeColumn mc:merges1) {
			if(mc.getStartRow() == mc.getEndRow())
				continue;
			CellRangeAddress region = new CellRangeAddress(mc.getStartRow(), mc.getEndRow(), mc.getStartColumn(), mc.getEndColumn());
			sheet.addMergedRegion(region);
		}
		for(MergeColumn mc:merges2) {
			if(mc.getStartRow() == mc.getEndRow())
				continue;
			CellRangeAddress region = new CellRangeAddress(mc.getStartRow(), mc.getEndRow(), mc.getStartColumn(), mc.getEndColumn());
			sheet.addMergedRegion(region);
		}
		try {
			merges1.clear();
			merges2.clear();
			workbook.write(stream);
			workbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	XSSFCell lastCell;
	MergeColumn mergeColumn;
	private void merge1(XSSFCell cell,List<MergeColumn> merges){
		if(lastCell == null) {
			lastCell = cell;
			mergeColumn = new MergeColumn();
			mergeColumn.setStartRow(cell.getRowIndex());
			mergeColumn.setStartColumn(cell.getColumnIndex());
		}
		if(!lastCell.getStringCellValue().equalsIgnoreCase(cell.getStringCellValue())) {
			mergeColumn.setEndRow(lastCell.getRowIndex());
			mergeColumn.setEndColumn(lastCell.getColumnIndex());
			merges.add(mergeColumn);
			
			mergeColumn = new MergeColumn();
			mergeColumn.setStartRow(cell.getRowIndex());
			mergeColumn.setStartColumn(cell.getColumnIndex());
		}
		lastCell = cell;
	}
	
	XSSFCell lastCell2;
	MergeColumn mergeColumn2;
	private void merge2(XSSFCell cell,List<MergeColumn> merges){
		if(lastCell2 == null) {
			lastCell2 = cell;
			mergeColumn2 = new MergeColumn();
			mergeColumn2.setStartRow(cell.getRowIndex());
			mergeColumn2.setStartColumn(cell.getColumnIndex());
		}
		if(!lastCell2.getStringCellValue().equalsIgnoreCase(cell.getStringCellValue())) {
			mergeColumn2.setEndRow(lastCell2.getRowIndex());
			mergeColumn2.setEndColumn(lastCell2.getColumnIndex());
			merges.add(mergeColumn2);
			
			mergeColumn2 = new MergeColumn();
			mergeColumn2.setStartRow(cell.getRowIndex());
			mergeColumn2.setStartColumn(cell.getColumnIndex());
		}
		lastCell2 = cell;
	}
	
	
	private String dateParse(Date date) {
		if(date == null) {
			return "";
		} else {
			return dateFormat2.format(date);
		}
	}
	
	private String progress(Float p) {
		if(p == null) {
			return "0%";
		} else {
			return p.intValue()+"%";
		}
	}
}
